package com.glasses.business.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.glasses.business.R;
import com.glasses.business.activity.LoginActiviy;
import com.glasses.business.activity.WebDetailActiviy;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.entity.User;
import com.glasses.business.event.BussEvent;
import com.glasses.business.fmanager.BaseFragment;
import com.glasses.business.http.NetUtils;
import com.glasses.business.utils.Tools;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/6
 * Version：1.0
 */

public class MeFragment extends BaseFragment {
    @InjectView(R.id.meSetting)
    ImageView meSetting;
    @InjectView(R.id.meMsg)
    ImageView meMsg;
    @InjectView(R.id.meName)
    TextView meName;
    @InjectView(R.id.meNumber)
    TextView meNumber;
    @InjectView(R.id.meUserLL)
    LinearLayout meUserLL;
    @InjectView(R.id.meLogin)
    TextView meLogin;
    @InjectView(R.id.meOrderRv)
    RelativeLayout meOrderRv;
    @InjectView(R.id.meLightOrderRv)
    RelativeLayout meLightOrderRv;
    @InjectView(R.id.addrRv)
    RelativeLayout addrRv;
    @InjectView(R.id.helpRv)
    RelativeLayout helpRv;
    @InjectView(R.id.suggestRv)
    RelativeLayout suggestRv;

    @Override
    public int getRootViewId() {
        return R.layout.fragment_me;
    }

    @Override
    public void setViews() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    public void onEventMainThread(BussEvent event) {
        if (event.getState() == BussEvent.Login_Out) {
            meUserLL.setVisibility(View.GONE);
            meLogin.setVisibility(View.VISIBLE);
        }else if(event.getState()==BussEvent.JPush){
            meMsg.setImageResource(R.mipmap.icon_xx);
        }
    }

    @Override
    public void initData() {
        if(MyApplication.getInstance().jpush){
            meMsg.setImageResource(R.mipmap.icon_xx2);
        }else{
            meMsg.setImageResource(R.mipmap.icon_xx);
        }
        User user = MyApplication.getInstance().user;
        if(user!=null){
            if (!TextUtils.isEmpty(user.getUserAcount())) {
                meNumber.setText(user.getUserAcount());
            }else{
                meNumber.setText("暂无");
            }
        }
    }

    @Override
    public boolean mainTabShow() {
        return true;
    }


    @OnClick({R.id.meSetting, R.id.meMsg, R.id.meLogin, R.id.meOrderRv, R.id.meLightOrderRv, R.id.addrRv, R.id.helpRv, R.id.suggestRv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.meSetting:
                open(new SettingFragment());
                break;
            case R.id.meMsg:
                open(new MsgListFragment());
                break;
            case R.id.meLogin:
                startActivity(new Intent(getActivity(), LoginActiviy.class));
                getActivity().finish();
                break;
            case R.id.meOrderRv:
                open(new MyOrderFragment());
                break;
            case R.id.meLightOrderRv:
                Bundle bundle = new Bundle();
                bundle.putBoolean("mine",true);
                open(new LightOrderrFragment(), bundle);
                break;
            case R.id.addrRv:
                open(new AddrFragment());
                break;
            case R.id.helpRv:
                Tools.showDialog(getActivity());
                NetUtils.getInstance().systemContent(0, new NetCallBack() {
                    @Override
                    public void onSuccess(String response, String mssage, ResultModel model) {
                        Tools.dismissWaitDialog();
                        ToastUtil.shortShowToast(mssage);
                        Bundle bundle = new Bundle();
                        bundle.putString("title","帮助说明");
                        bundle.putString("url",response);
                        Intent intent = new Intent(getActivity(), WebDetailActiviy.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }

                    @Override
                    public void onFail(boolean dataError, int errorCode, String mssage) {
                        Tools.dismissWaitDialog();
                        ToastUtil.shortShowToast(mssage);
                    }
                },null);
                break;
            case R.id.suggestRv:
                open(new SuggestFragment());
                break;
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
